$import( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeNewParam" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeConstant" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeLambert" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaePhong" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeBlinn" );

SmitearWebGL.Objects.Parser.Collada.DaeEffect = SmitearWebGL.Objects.Parser.Collada.DaeEntity.extend(
{
	/******** constructor ********/

	init : function( document, node )
	{
		this.shader = null;
		this.newparams = null;
		this.texture_url = "";
		this.double_sided = false;
		this.wireframe = false;

		this._super( document, node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeEffect" );
	}
	,
	read : function( node )
	{
		this._super( node );

		// The <profile_COMMON> elements encapsulate all the values and declarations for
		// a platform-independent fixed-function shader. All platforms are required to
		// support <profile_COMMON>. <profile_COMMON> effects are designed to be used as
		// the reliable fallback when no other profile is recognized by the current
		// effects runtime.
		var profile = this.getNode( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_PROFILE_COMMON_ELEMENT );
		
		$THROW( profile, "Can't handle profiles other then profile_COMMON!" );

		// Declares a standard COLLADA image resource. 0 or more.
		var images = this.getNodeList( profile, SmitearWebGL.Objects.Parser.Collada.Const.DAE_IMAGE_ELEMENT );

		// Creates a new parameter from a constrained set of
		// types recognizable by all platforms <float>,
		// <float2>, <float3>, <float4>, <surface>, and
		// <sampler2D>, with an additional semantic. 0 or more.
		var newparams = this.getNodeList( profile, SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXCMN_NEWPARAM_ELEMENT );

		// Declares the only program for this effect. This node
		// contains <asset>, <image>, and <extra>, along
		// with one of <constant>, <lambert>, <phong>, or
		// <blinn>.
		var program = this.getNode( profile, SmitearWebGL.Objects.Parser.Collada.Const.DAE_TECHNIQUE_ELEMENT );

		// check for a SID
		var techSID = program.getAttribute( SmitearWebGL.Objects.Parser.Collada.Const.DAE_SID_ATTRIBUTE );

		$LOG( "reading effect: " + this.id );
		$LOG( " => #images: " + images.length );
		$LOG( " => #newparams: " + newparams.length );
		$LOG( " => program sid: " + techSID );

		this.newparams = {};
		for ( var i = 0; i < newparams.length; ++ i )
		{
			var p = new SmitearWebGL.Objects.Parser.Collada.DaeNewParam( this.document, newparams[i] );
			this.newparams[ p.type ] = p;
		}

		var phong = this.getNode( program, SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXSTD_PHONG_ELEMENT );
		var lambert = this.getNode( program, SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXSTD_LAMBERT_ELEMENT );
		var blinn = this.getNode( program, SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXSTD_BLINN_ELEMENT );
		var constant = this.getNode( program, SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXSTD_CONSTANT_ELEMENT );
		
		if( phong )
		{
			$LOG( " => shader: phong" );
			this.shader = new SmitearWebGL.Objects.Parser.Collada.DaePhong( this.document, phong );
		}
		else if( lambert )
		{
			$LOG( " => shader: lambert" );
			this.shader = new SmitearWebGL.Objects.Parser.Collada.DaeLambert( this.document, lambert );
		}
		else if( blinn )
		{
			$LOG( " => shader: blinn" );
			this.shader = new SmitearWebGL.Objects.Parser.Collada.DaeBlinn( this.document, blinn );
		}
		else if( constant )
		{
			$LOG( " => shader: constant" );
			this.shader = new SmitearWebGL.Objects.Parser.Collada.DaeConstant( this.document, constant );
		}
		var extra = this.getNodeList( program, SmitearWebGL.Objects.Parser.Collada.Const.DAE_EXTRA_ELEMENT )[1];
		if ( extra )
		{
			var speNode = this.getNode( extra, SmitearWebGL.Objects.Parser.Collada.Const.DAEMAX_SPECLEVEL_MATERIAL_PARAMETER );
			if ( speNode ) this.shader.speLevel = this.getFloats( this.getNode( speNode, SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXCMN_FLOAT_ELEMENT ) )[0];
			else this.shader.speLevel = 8.0;
		}
		else
			this.shader.speLevel = 8.0;
		
		var surface = this.newparams[SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXCMN_SURFACE_ELEMENT];
		var sampler2D = this.newparams[SmitearWebGL.Objects.Parser.Collada.Const.DAE_FXCMN_SAMPLER2D_ELEMENT];

		var ph = this.shader;
		if( ph && ph.diffuse && ph.diffuse.type == SmitearWebGL.Objects.Parser.Collada.DaeColorOrTexture.TYPE_TEXTURE && sampler2D && surface )
		{
			if( sampler2D.sid == ph.diffuse.texture.texture && sampler2D.sampler2D.source == surface.sid )
			{
				this.texture_url = surface.surface.init_from;
			}
		}

		this.readExtra( node );
	}
	,
	readExtra : function( node )
	{
		var extraList = this.getNodeList( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_EXTRA_ELEMENT );

		for ( var i = 0; i < extraList.length; ++ i )
		{
			var extraNode = extraList[i];
			var programNode = this.getNode( extraNode, SmitearWebGL.Objects.Parser.Collada.Const.DAE_TECHNIQUE_ELEMENT );
			var profile = programNode.getAttribute( SmitearWebGL.Objects.Parser.Collada.Const.DAE_PROFILE_ATTRIBUTE );
			var tmp;
			var text;

			switch( profile )
			{
				case SmitearWebGL.Objects.Parser.Collada.Const.DAEMAX_MAX_PROFILE:
					tmp = this.getNode( programNode, SmitearWebGL.Objects.Parser.Collada.Const.DAESHD_DOUBLESIDED_PARAMETER );
					if( tmp )
					{
						text = this.getNodeContent( tmp );
						this.double_sided = (text == "1" || text == "true");
					}
					tmp = this.getNode( programNode, SmitearWebGL.Objects.Parser.Collada.Const.DAEMAX_WIREFRAME_MATERIAL_PARAMETER );
					if( tmp )
					{
						text = this.getNodeContent( tmp );
						this.wireframe = (text == "1" || text == "true");
					}
					break;
					
				default:
					break;
			}
		}
	}
}
);